﻿using Himp.Outpatient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.ObjectMapping;

namespace Himp.Outpatient
{
    /// <summary>
    /// 门诊排班时段
    /// </summary>
    public class OtpSchdPeriodService : OutpatientAppService
    {
        private readonly IOtpSchdPlanRepository _optSchdPlanRepository;

        public OtpSchdPeriodService(IOtpSchdPlanRepository optSchdPlanRepository)
        {
            _optSchdPlanRepository = optSchdPlanRepository;
        }

        /// <summary>
        /// 创建门诊排班时段
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<OtpSchdPlanDto> PostAsync(OtpSchdPeriodCreateDto input)
        {
            var optSchdPlan = await _optSchdPlanRepository.GetAsync(input.OtpSchdPlanId);
            var optSchdPeriod = ObjectMapper.Map<OtpSchdPeriodCreateDto, OtpSchdPeriod>(input);
            optSchdPlan.OtpSchdPeriods = optSchdPlan.OtpSchdPeriods ?? new List<OtpSchdPeriod>();
            optSchdPlan.OtpSchdPeriods.Add(optSchdPeriod);
            optSchdPlan = await _optSchdPlanRepository.UpdateAsync(optSchdPlan);
            var itemDto = ObjectMapper.Map<OtpSchdPlan, OtpSchdPlanDto>(optSchdPlan);

            return itemDto;
        }

        /// <summary>
        /// 修改门诊排班时段
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<OtpSchdPlanDto> PutAsync(Guid id, OtpSchdPeriodUpdateDto input)
        {
            var optSchdPlan = await _optSchdPlanRepository.GetAsync(input.OtpSchdPlanId);
            var optSchdPeriod = optSchdPlan.OtpSchdPeriods.FirstOrDefault(o=>o.Id == id);
            optSchdPeriod = ObjectMapper.Map(input, optSchdPeriod);
            optSchdPlan = await _optSchdPlanRepository.UpdateAsync(optSchdPlan);
            var itemDto = ObjectMapper.Map<OtpSchdPlan, OtpSchdPlanDto>(optSchdPlan);

            return itemDto;
        }

        /// <summary>
        /// 删除门诊排班时段
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task DeleteAsync(Guid id, OtpSchdPeriodDeleteDto input)
        {
            var optSchdPlan = await _optSchdPlanRepository.GetAsync(input.OtpSchdPlanId);
            var optSchdPeriod = optSchdPlan.OtpSchdPeriods.FirstOrDefault(o => o.Id == id);
            optSchdPlan.OtpSchdPeriods.Remove(optSchdPeriod);
            optSchdPlan = await _optSchdPlanRepository.UpdateAsync(optSchdPlan);
            var itemDto = ObjectMapper.Map<OtpSchdPlan, OtpSchdPlanDto>(optSchdPlan);
        }
    }
}
